/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.shop.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUserInner;
import com.pig4cloud.pigx.shop.user.service.ShopUserInnerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 内部员工表
 *
 * @author pigx code generator
 * @date 2024-01-23 17:26:26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user/inner")
@Tag(description = "shop_user_inner", name = "内部员工表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ShopUserInnerController {

	private final ShopUserInnerService shopUserInnerService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param shopUserInner 内部员工表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R getShopUserInnerPage(Page page, ShopUserInner shopUserInner) {
		LambdaQueryWrapper<ShopUserInner> lqwShopUserInner = new LambdaQueryWrapper<>();
		if (!StringUtils.isEmpty(shopUserInner.getUserNo())) {
			lqwShopUserInner.eq(ShopUserInner::getUserNo, shopUserInner.getUserNo());
		}
		if (!StringUtils.isEmpty(shopUserInner.getPhone())) {
			lqwShopUserInner.eq(ShopUserInner::getPhone, shopUserInner.getPhone());
		}
		lqwShopUserInner.orderByDesc(ShopUserInner::getCreateTime);
		return R.ok(shopUserInnerService.page(page, lqwShopUserInner));
	}

	/**
	 * 通过id查询内部员工表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(shopUserInnerService.getById(id));
	}

	/**
	 * 新增内部员工表
	 * @param shopUserInner 内部员工表
	 * @return R
	 */
	@Operation(summary = "新增内部员工表", description = "新增内部员工表")
	@SysLog("新增内部员工表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('shop_user_inner_add')")
	public R save(@RequestBody ShopUserInner shopUserInner) {
		return R.ok(shopUserInnerService.save(shopUserInner));
	}

	/**
	 * 修改内部员工表
	 * @param shopUserInner 内部员工表
	 * @return R
	 */
	@Operation(summary = "修改内部员工表", description = "修改内部员工表")
	@SysLog("修改内部员工表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('shop_user_inner_edit')")
	public R updateById(@RequestBody ShopUserInner shopUserInner) {
		return R.ok(shopUserInnerService.updateById(shopUserInner));
	}

	/**
	 * 通过id删除内部员工表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除内部员工表", description = "通过id删除内部员工表")
	@SysLog("通过id删除内部员工表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('shop_user_inner_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(shopUserInnerService.removeById(id));
	}

	/**
	 * 导出excel 表格
	 * @param shopUserInner 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('shop_user_inner_export')")
	public List<ShopUserInner> export(ShopUserInner shopUserInner) {
		return shopUserInnerService.list(Wrappers.query(shopUserInner));
	}

	/**
	 * 内部员工登录
	 * @param shopUserInner
	 * @return
	 */
	@Inner(false)
	@PostMapping("/c/login")
	public R login(@RequestBody ShopUserInner shopUserInner) {

		if (shopUserInner.getType().equals("2")) {
			// 特惠码登录的场合
			if (StringUtils.isEmpty(shopUserInner.getCouponCode())) {
				return R.failed("特惠码不能为空");
			}
			LambdaQueryWrapper<ShopUserInner> lqwShopUserInner = new LambdaQueryWrapper<>();
			lqwShopUserInner.eq(ShopUserInner::getCouponCode, shopUserInner.getCouponCode());
			List<ShopUserInner> userInfo = shopUserInnerService.list(lqwShopUserInner);
			if (userInfo.size() <= 0) {
				return R.failed("特惠码不正确，无法登录");
			}
		}
		else {
			// 员工登录的场合
			if (StringUtils.isEmpty(shopUserInner.getUserNo())) {
				return R.failed("员工编号不能为空");
			}

			if (StringUtils.isEmpty(shopUserInner.getPhone())) {
				return R.failed("员工手机号不能为空");
			}
			LambdaQueryWrapper<ShopUserInner> lqwShopUserInner = new LambdaQueryWrapper<>();
			lqwShopUserInner.eq(ShopUserInner::getPhone, shopUserInner.getPhone());
			lqwShopUserInner.eq(ShopUserInner::getUserNo, shopUserInner.getUserNo());
			List<ShopUserInner> userInfo = shopUserInnerService.list(lqwShopUserInner);
			if (userInfo.size() <= 0) {
				return R.failed("员工信息不正确，无法登录");
			}
		}
		return R.ok();
	}

}
